﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace EasyUIMvc
{
    public class DataGridExtension : ExtensionBase
    {
        private readonly DataGridSettings _settings;
        private IDictionary<string, string> _rows;

        public override string GetScripts()
        {
            throw new NotImplementedException();
        }
        public DataGridExtension(DataGridSettings settings) {
            _settings = settings;
        }
        public DataGridExtension Bind(IDictionary<string, string> rows) {
            _rows = rows;
            return this;
        }
        public override MvcHtmlString GetHtml()
        {
            var datagrid = GetTopTag("table", "easyui-datagrid", _settings);
            datagrid.MergeAttribute("title", _settings.Title);
            var thead = new TagBuilder("thead");
            var tr = new TagBuilder("tr");
            foreach (KeyValuePair<string, string> row in _rows)
            {
                var th = new TagBuilder("th");
                th.MergeAttribute("data-options", row.Key);
                th.SetInnerText(row.Value);
                tr.InnerHtml += th;
            }
            thead.InnerHtml += tr;
            datagrid.InnerHtml += thead;
            return MvcHtmlString.Create(datagrid.ToString());
        }

    }
    public class DataGridSettings : SettingsBase 
    {
        public string Title { get; set; }
    }
}
